package com.keyboard
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	public class KeyboardManager
	{
		private var stage:Stage;
		
		private var _func:Object = {}; // 每个健只能被一个处理函数监听
		private var _ctrlFunc:Object = {}; // 目前只有简单的组合健
		private var _altFunc:Object = {};
		private var _shiftFunc:Object = {};
		private var _funcUp:Object = {}; // 鼠标弹起的时候调用
		
		private var _keyDown:Object = {};
		
		private static var _instance:KeyboardManager;
		public static function getInstance():KeyboardManager{
			if(!_instance){
				_instance = new KeyboardManager;
			}
			return _instance;
		}
		
		public function KeyboardManager()
		{
		}
		
		/**
		 * 使用前先初始化
		 */
		public function init(stage:Stage):void{
			this.stage = stage;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
		}
		
		private function keyDown(event:KeyboardEvent):void{
			var key:int = event.keyCode;
			
			_keyDown[key] = true;
			
			var funcObj:KeyboardObject;
			if(event.altKey){
				funcObj = _altFunc[key];
			}else if(event.ctrlKey){
				funcObj = _ctrlFunc[key];
			}else if(event.shiftKey){
				funcObj = _shiftFunc[key];
			}else{
				funcObj = _func[key];
			}
			if(funcObj){
				funcObj.func.apply(null, funcObj.args);
			}
		}
		
		private function keyUp(event:KeyboardEvent):void{
			var key:int = event.keyCode;
			
			delete _keyDown[key];
			
			var funcObj:KeyboardObject;
			if(event.altKey){
				
			}else if(event.ctrlKey){

			}else if(event.shiftKey){

			}else{
				funcObj = _funcUp[key];
			}
			if(funcObj){
				funcObj.func.apply(null, funcObj.args);
			}
		}
		
		public function isDown(key:int):Boolean{
			if(_keyDown[key]){
				return true;
			}
			return false;
		}
		
		/**
		 * func = null 得话相当与注销监听
		 * key 是键盘代码，没有大小写之分
		 */
		public function setFuncCode(key:int, func:Function, ...args):void{
			if(func){
				_func[key] = new KeyboardObject(func, args);
			}else{
				delete _func[key];
			}
		}
		
		/**
		 * 不分大小写
		 */
		public function setFunc(char:String, func:Function, ...args):void{
			var key:int = getCharCode(char);
			if(func){
				_func[key] = new KeyboardObject(func, args);
			}else{
				delete _func[key];
			}
		}
		
		/**
		 * 键盘弹起事件
		 */
		public function setFuncCodeUp(key:int, func:Function, ...args):void{
			if(func){
				_funcUp[key] = new KeyboardObject(func, args);
			}else{
				delete _funcUp[key];
			}
		}
		
		/**
		 * 键盘弹起事件
		 */
		public function setFuncUp(char:String, func:Function, ...args):void{
			var key:int = getCharCode(char);
			if(func){
				_funcUp[key] = new KeyboardObject(func, args);
			}else{
				delete _funcUp[key];
			}
		}
		
		
		public function setCtrlFunc(char:String, func:Function, ...args):void{
			var key:int = getCharCode(char);
			if(func){
				_ctrlFunc[key] = new KeyboardObject(func, args);
			}else{
				delete _ctrlFunc[key];
			}
		}
		
		public function setAltFunc(char:String, func:Function, ...args):void{
			var key:int = getCharCode(char);
			if(func){
				_altFunc[key] = new KeyboardObject(func, args);
			}else{
				delete _altFunc[key];
			}
		}
		
		public function setShiftFunc(char:String, func:Function, ...args):void{
			var key:int = getCharCode(char);
			if(func){
				_shiftFunc[key] = new KeyboardObject(func, args);
			}else{
				delete _shiftFunc[key];
			}
		}
		
		public function getCharCode(char:String):int{
			return (char.toUpperCase()).charCodeAt(0);
		}
	}
}